package com.ocr.fragment;

import com.ocr.R;
import com.ocr.navigator.INavigateToFragmentResult;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class FragmentResult extends Fragment implements
		INavigateToFragmentResult {
	private String result;
	private EditText editTextResult;
	private ImageView imgCopy;
	private ImageView imgBack;
	private ImageView imgShare;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View root = inflater
				.inflate(R.layout.fragment_result, container, false);
		// find view by id
		editTextResult = (EditText) root.findViewById(R.id.txt_result);
		imgBack = (ImageView) root.findViewById(R.id.imgBack);
		imgCopy = (ImageView) root.findViewById(R.id.imgCopy);
		imgShare = (ImageView) root.findViewById(R.id.imgShare);
		editTextResult.setText(result);
		// listener
		imgBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				getFragmentManager().popBackStack();
			}
		});
		imgCopy.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ClipboardManager clipboard = (ClipboardManager) getActivity()
						.getSystemService(Activity.CLIPBOARD_SERVICE);
				ClipData clip = ClipData.newPlainText("label",
						editTextResult.getText());
				clipboard.setPrimaryClip(clip);
				Toast.makeText(getActivity(),getResources().getString(R.string.copyed), Toast.LENGTH_SHORT).show();;
				
			}
		});
		imgShare.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent sharingIntent = new Intent(
						android.content.Intent.ACTION_SEND);
				sharingIntent.setType("text/plain");
				String rsText = editTextResult.getText().toString();
				sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT,
						rsText);
				startActivity(Intent.createChooser(sharingIntent, "Share"));
			}
		});
		return root;
	}

	@Override
	public void navigateToFragmentResult(String result) {
		// TODO Auto-generated method stub
		this.result = result;
	}
}
